<template>
  <div class="repair-list">
    <div class="header">
      <div class="title">我的维修单</div>
      <div class="refresh-btn">
        <el-button 
          type="primary" 
          icon="el-icon-refresh" 
          circle
          @click="refreshList"
          :loading="loading">
        </el-button>
      </div>
    </div>

    <el-table 
      v-loading="loading"
      :data="repairs" 
      stripe
      class="repair-table"
      @row-click="showDetail">
      <el-table-column prop="rId" label="维修单号" width="120"></el-table-column>
      <el-table-column prop="pcName" label="设备名称" width="140"></el-table-column>
      <el-table-column prop="reason" label="问题描述" min-width="200"></el-table-column>
      <el-table-column label="报修图片" width="100">
        <template slot-scope="scope">
          <el-image 
            v-if="scope.row.imgUrl"
            style="width: 60px; height: 60px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]">

          </el-image>
          <span v-else>无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" width="130">
        <template slot-scope="scope">
  <div v-if="scope.row.status === 0" class="status-info">
    <span class="status-text">未分配</span>
  </div>
  <div v-else-if="scope.row.status === 1" class="repairer-info">
    <span class="status-text">处理中</span>
  </div>
  <div v-else-if="scope.row.status === 2" class="status-info">
    <span class="status-text">已完成</span>

  </div>
</template>
      </el-table-column>
      <el-table-column prop="remark" label="维修备注" width="150"></el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button 
            v-if="scope.row.status === '待处理'"
            type="text" 
            size="small"
            @click="cancelRepair(scope.row)"
            style="color: #F56C6C;">
            取消
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="showDetail(scope.row)">
            详情
          </el-button>
          <el-button
            type="text"
            size="small"
            style="color: #F56C6C;"
            @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 维修单详情对话框 -->
    <el-dialog
      title="维修单详情"
      :visible.sync="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
      custom-class="repair-dialog">
      <div v-if="currentRepair" class="repair-detail">
        <div class="detail-item">
          <span class="label">维修单号：</span>
          <span>{{ currentRepair.rIId }}</span>
        </div>
        <div class="detail-item">
          <span class="label">设备名称：</span>
          <span>{{ currentRepair.pcName }}</span>
        </div>
        <div class="detail-item">
          <span class="label">问题描述：</span>
          <span>{{ currentRepair.reason }}</span>
        </div>
        <div class="detail-item">
          <span class="label">报修图片：</span>
          <el-image 
            v-if="currentRepair.img"
            style="width: 200px; height: 200px"
            :src="currentRepair.imgUrl"
            :preview-src-list="[currentRepair.img]">
          </el-image>
          <span v-else>无图片</span>
        </div>
        <div class="detail-item">
          <span class="label">维修人员：</span>
          <div v-if="currentRepair.repairerName" class="repairer-info">
            <el-avatar :size="30" :src="currentRepair.repairerAvatar"></el-avatar>
            <span class="repairer-name">{{ currentRepair.repairerName }}</span>
          </div>
          <span v-else>待分配</span>
        </div>
        <div class="detail-item">
          <span class="label">当前状态：</span>
          <el-tag :type="getStatusType(currentRepair.status)">{{ currentRepair.status }}</el-tag>
        </div>
        <div class="detail-item">
          <span class="label">提交时间：</span>
          <span>{{ currentRepair.createTime }}</span>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'MyRepairList',
  data() {
    return {
      repairs: [],
      dialogVisible: false,
      currentRepair: null,
      loading: false
    }
  },
  created() {
    this.loadRepairs()
  },
  methods: {
    async loadRepairs() {
      this.loading = true
      try {
        const user = JSON.parse(localStorage.getItem('xm-user'))
        const res = await this.$request.get('/repairer/selectByUser', {
          params: { userId: user.id }
        })
        
        if (res.code === '200') {
          this.repairs = res.data
        } else {
          this.$message.error('获取维修单列表失败')
        }
      } catch (err) {
        console.error('获取维修单列表失败:', err)
        this.$message.error('获取维修单列表失败')
      } finally {
        this.loading = false
      }
    },
    getStatusType(status) {
      const types = {
        '待处理': 'warning',
        '处理中': 'primary',
        '已完成': 'success',
        '已取消': 'info'
      }
      return types[status] || ''
    },
    async cancelRepair(row) {
      try {
        await this.$confirm('确认取消该维修单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        const res = await this.$request.post('/repairer/update', {
          id: row.id,
          status: '已取消'
        })
        
        if (res.code === '200') {
          this.$message.success('维修单已取消')
          this.loadRepairs()
        } else {
          this.$message.error(res.msg || '取消维修单失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('取消维修单失败')
        }
      }
    },
    showDetail(repair) {
      this.currentRepair = repair
      this.dialogVisible = true
    },
    async handleDelete(row) {
      try {
        await this.$confirm('此操作将永久删除该维修单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        const res = await this.$request.post(`/repairer/delete/${row.id}`)
        
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.loadRepairs()
        } else {
          this.$message.error(res.msg || '删除失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除维修单失败:', error)
          this.$message.error('删除失败')
        }
      }
    },
    refreshList() {
      this.loadRepairs()
    }
  }
}
</script>

<style scoped>
.repair-list {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  min-height: 500px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.title {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  position: relative;
  padding-left: 12px;
}

.title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 20px;
  background-color: #409EFF;
  border-radius: 2px;
}

.repair-table {
  transition: all 0.3s ease;
  margin-top: 15px;
}

.repair-table:hover {
  transform: translateY(-2px);
}

/* 表格行悬浮效果 */
.el-table__row {
  cursor: pointer;
  transition: all 0.3s ease;
}

.el-table__row:hover {
  background-color: #f0f7ff !important;
}

/* 按钮动效 */
.el-button {
  transition: all 0.3s ease;
}

.el-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* 对话框动画 */
.repair-dialog {
  border-radius: 8px;
  overflow: hidden;
}

.repair-detail {
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 标签动效 */
.el-tag {
  transition: all 0.3s ease;
}

.el-tag:hover {
  transform: scale(1.05);
}

/* 图片预览优化 */
.el-image {
  transition: all 0.3s ease;
  border-radius: 4px;
  overflow: hidden;
}

.el-image:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* 滚动条美化 */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-thumb {
  background: #c0c4cc;
  border-radius: 3px;
}

::-webkit-scrollbar-track {
  background: #f5f5f5;
}

/* 头像动效 */
.el-avatar {
  transition: all 0.3s ease;
}

.el-avatar:hover {
  transform: scale(1.1);
}

/* 加载动画容器 */
.el-loading-mask {
  background-color: rgba(255,255,255,0.9);
}

.repairer-info {
  display: flex;
  align-items: center;
  gap: 6px;
}

.repairer-name {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.repair-detail {
  padding: 10px;
}

.detail-item {
  margin-bottom: 15px;
  display: flex;
  align-items: flex-start;
}

.detail-item .label {
  width: 100px;
  color: #666;
  text-align: right;
  margin-right: 10px;
}

/* 优化表格样式 */
.repair-table {
  margin-top: 15px;
}

/* 调整维修人员列的样式 */
.repairer-info {
  display: flex;
  align-items: center;
  gap: 6px;
}

.repairer-name {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 调整问题描述列的样式 */
.el-table .cell {
  line-height: 1.5;
}

/* 让表格更紧凑 */
.el-table td, .el-table th {
  padding: 8px 0;
}

/* 优化按钮组样式 */
.el-button--text {
  padding: 0 8px;
}

.el-button--text + .el-button--text {
  margin-left: 8px;
}
</style> 